package logic

import (
	"context"
	"modbus/rpcServer/internal/svc"
	"modbus/rpcServer/types/iotrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetDeviceTableListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetDeviceTableListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDeviceTableListLogic {
	return &GetDeviceTableListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetDeviceTableListLogic) GetDeviceTableList(in *iotrpc.Null) (*iotrpc.DeviceListReplay, error) {
	// todo: add your logic here and delete this line
	//replay := iotrpc.DeviceListReplay{}
	//reply := iotrpc.DeviceReply{}
	//reply.DevName = "pcs"
	//reply.InterName = "pcs"
	//replay.Devlist = append(replay.Devlist, &reply)
	//reply.DevName = "pcs1"
	//reply.InterName = "pcs2"
	//replay.Devlist = append(replay.Devlist, &reply)
	replay := iotrpc.DeviceListReplay{}
	for _, table := range *l.svcCtx.DevMap {
		reply := iotrpc.DeviceReply{}
		reply.DevId = int32(table.DevInfo.DevId)
		reply.DevName = table.DevInfo.DevName
		reply.InterName = table.DevInfo.InterName
		reply.Address = table.DevInfo.Address
		replay.Devlist = append(replay.Devlist, &reply)
	}

	return &replay, nil
}
